package com.geekaca.web.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.geekaca.web.service.BrandService;
import com.geekaca.web.utils.Result;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/batchDel")
public class BatchDelServlet extends HttpServlet {
    private BrandService brandService = new BrandService();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletInputStream ips = req.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(ips));
        String line = br.readLine();
        System.out.println("line: " + line);
        JSONArray jsonArray = JSON.parseObject(line, JSONArray.class);
        Integer[] ids = jsonArray.toArray(new Integer[jsonArray.size()]);
        // 执行批量删除
        Integer deleted = brandService.batchDel(ids);

        if(deleted!=null){
            resp.setContentType("text/json;charset=utf-8");
            PrintWriter writer = resp.getWriter();
            Result result = new Result();
            result.setCode(200);
            result.setMsg("删除成功");
            writer.write(JSON.toJSONString(result));
            writer.flush();
        }else {
            resp.setContentType("text/json;charset=utf-8");
            PrintWriter writer = resp.getWriter();
            Result result = new Result();
            result.setCode(500);
            result.setMsg("删除失败");
            writer.write(JSON.toJSONString(result));
            writer.flush();
        }


    }

}
